package com.example.project3;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.project3.model.ListItem;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 详情页Activity
 */
public class DetailActivity extends AppCompatActivity {
    private ImageView ivDetailImage;
    private TextView tvDetailTitle;
    private TextView tvDetailSubtitle;
    private TextView tvDetailDescription;
    private TextView tvDetailTime;
    private ListItem item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        
        // 初始化视图
        initViews();
        
        // 获取传递的数据
        getIntentData();
        
        // 显示数据
        displayData();
        
        // 设置返回按钮
        setupActionBar();
    }

    /**
     * 初始化视图
     */
    private void initViews() {
        ivDetailImage = findViewById(R.id.iv_detail_image);
        tvDetailTitle = findViewById(R.id.tv_detail_title);
        tvDetailSubtitle = findViewById(R.id.tv_detail_subtitle);
        tvDetailDescription = findViewById(R.id.tv_detail_description);
        tvDetailTime = findViewById(R.id.tv_detail_time);
    }

    /**
     * 获取Intent传递的数据
     */
    private void getIntentData() {
        item = (ListItem) getIntent().getSerializableExtra("item");
        if (item == null) {
            // 如果没有数据，关闭当前页面
            finish();
        }
    }

    /**
     * 显示数据
     */
    private void displayData() {
        if (item != null) {
            tvDetailTitle.setText(item.getTitle());
            tvDetailSubtitle.setText(item.getSubtitle());
            tvDetailDescription.setText(item.getDescription());
            
            // 显示时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            String timeText = "创建时间：" + sdf.format(new Date(item.getTimestamp()));
            tvDetailTime.setText(timeText);
            
            // 设置默认图片
            ivDetailImage.setImageResource(R.drawable.ic_launcher_foreground);
        }
    }

    /**
     * 设置操作栏
     */
    private void setupActionBar() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("详情");
        }
    }

    /**
     * 处理返回按钮点击
     */
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}